﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using XFSale.Lib;

namespace XFSale.UI
{
    using MenuMetaSet = List<MenuMeta>;

    public partial class WinKey : Form
    {
        public WinKey()
        {
            InitializeComponent();
        }

        private void WinKeyMenu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                try
                {
                    foreach (MenuMeta menu in App.MenuMeta)
                    {
                        string hotKey = TxtMenuHotKey.Text.Trim();
                        if (!string.IsNullOrEmpty(hotKey) && menu.menuHotKey.EndsWith(hotKey))
                        {
                            DevExpress.XtraEditors.SplitContainerControl Splitter =
                            (Owner as WinMain).Controls["Splitter"] as DevExpress.XtraEditors.SplitContainerControl;

                            if (Splitter.Panel2.Controls.Count > 0)
                                return;
                            
                            /*if (!Splitter.Panel1.Enabled)
                                return;*/

                            //Splitter.Panel1.Enabled = false;
                            //NavPanel.Enabled = false;

                            /*foreach (Control ctrl in Splitter.Panel2.Controls)
                            {
                                ctrl.Dispose();
                            }*/

                            string dll = null;
                            if (!string.IsNullOrEmpty(menu.callDLL))
                                dll = menu.callDLL;

                            FrmBase frame = Activator.CreateInstance(dll, menu.callFunc).Unwrap() as FrmBase;
                            frame.Name = menu.menuID;
                            frame.Tag = menu.callParam;
                            frame.Dock = DockStyle.Left;
                            Splitter.Panel2.Controls.Add(frame);
                            break;
                        }
                    }
                }
                catch
                {
                }
                finally
                {
                    this.Close();
                }
            }
        }
    }
}
